package com.leyou.auth.config;

import com.leyou.common.auth.RsaUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.security.PrivateKey;
import java.security.PublicKey;

/**
 * @author 虎哥
 */
@Slf4j
@Data
@ConfigurationProperties(prefix = "ly.jwt")
public class JwtProperties implements InitializingBean {
    /**
     * 公钥的地址
     */
    private String pubKeyPath;
    /**
     * 私钥的地址
     */
    private String priKeyPath;
    /**
     * 公钥对象
     */
    public PublicKey publicKey;
    /**
     * 私钥对象
     */
    public PrivateKey privateKey;

    @Override
    public void afterPropertiesSet() throws Exception {
        try {
            publicKey = RsaUtils.getPublicKey(pubKeyPath);
            privateKey = RsaUtils.getPrivateKey(priKeyPath);
        } catch (Exception e) {
            log.error("加载公钥和私钥失败！", e);
            throw new RuntimeException(e);
        }
    }

    /*{
        try {
            publicKey = RsaUtils.getPublicKey(pubKeyPath);
            privateKey = RsaUtils.getPrivateKey(priKeyPath);
        } catch (Exception e) {
            log.error("加载公钥和私钥失败！", e);
            throw new RuntimeException(e);
        }
    }*/
}
